package com.sugar.follow.up.module.business.controller.admin.followuptemplate;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.pojo.CommonResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;
import static com.sugar.follow.up.framework.common.pojo.CommonResult.success;

import com.sugar.follow.up.framework.excel.core.util.ExcelUtils;

import com.sugar.follow.up.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.follow.up.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.follow.up.module.business.controller.admin.followuptemplate.vo.*;
import com.sugar.follow.up.module.business.dal.dataobject.followuptemplate.FollowUpTemplateDO;
import com.sugar.follow.up.module.business.service.followuptemplate.FollowUpTemplateService;

@Tag(name = "管理后台 - 随访模板")
@RestController
@RequestMapping("/business/follow-up-template")
@Validated
public class FollowUpTemplateController {

    @Resource
    private FollowUpTemplateService followUpTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建随访模板")
    @PreAuthorize("@ss.hasPermission('business:follow-up-template:create')")
    public CommonResult<Long> createFollowUpTemplate(@Valid @RequestBody FollowUpTemplateSaveReqVO createReqVO) {
        return success(followUpTemplateService.createFollowUpTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新随访模板")
    @PreAuthorize("@ss.hasPermission('business:follow-up-template:update')")
    public CommonResult<Boolean> updateFollowUpTemplate(@Valid @RequestBody FollowUpTemplateSaveReqVO updateReqVO) {
        followUpTemplateService.updateFollowUpTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除随访模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:follow-up-template:delete')")
    public CommonResult<Boolean> deleteFollowUpTemplate(@RequestParam("id") Long id) {
        followUpTemplateService.deleteFollowUpTemplate(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除随访模板")
                @PreAuthorize("@ss.hasPermission('business:follow-up-template:delete')")
    public CommonResult<Boolean> deleteFollowUpTemplateList(@RequestParam("ids") List<Long> ids) {
        followUpTemplateService.deleteFollowUpTemplateListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得随访模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:follow-up-template:query')")
    public CommonResult<FollowUpTemplateRespVO> getFollowUpTemplate(@RequestParam("id") Long id) {
        FollowUpTemplateDO followUpTemplate = followUpTemplateService.getFollowUpTemplate(id);
        return success(BeanUtils.toBean(followUpTemplate, FollowUpTemplateRespVO.class));
    }

    @GetMapping("/all")
    @Operation(summary = "获得所有随访模板")
    public CommonResult<List<FollowUpTemplateSimpleRespVO>> getFollowUpTemplateAll() {
        return success(followUpTemplateService.getFollowUpTemplateAll());
    }

    @GetMapping("/page")
    @Operation(summary = "获得随访模板分页")
    @PreAuthorize("@ss.hasPermission('business:follow-up-template:query')")
    public CommonResult<PageResult<FollowUpTemplateRespVO>> getFollowUpTemplatePage(@Valid FollowUpTemplatePageReqVO pageReqVO) {
        PageResult<FollowUpTemplateDO> pageResult = followUpTemplateService.getFollowUpTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FollowUpTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出随访模板 Excel")
    @PreAuthorize("@ss.hasPermission('business:follow-up-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFollowUpTemplateExcel(@Valid FollowUpTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        followUpTemplateService.exportFollowUpTemplateExcel(pageReqVO, response);
    }

}